package com.jumi.microservice.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jm.member.common.redis.service.RedisService;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.entity.JmLogout;
import com.jumi.microservice.enums.CheckStatus;
import com.jumi.microservice.service.IJmJpushUserService;
import com.jumi.microservice.service.IJmLogoutService;
import com.jumi.microservice.service.IJmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/logout")
@Api(value = "C-用户注销，退出登录", tags = "C-用户注销，退出登录")
public class JmLogOutController {

    @Resource
    IJmLogoutService logoutService;

    @Resource
    IJmUserService userService;

    @Resource
    RedisService redisService;

    @Resource
    IJmJpushUserService jpushUserService;


    @PostMapping("/apply_for_cancellation")
    @ApiOperation(value = "申请注销账号")
    public ResponseResult logout(@ApiParam(required = true, value = "注销理由") @RequestParam String applyReason) {
        Integer memberId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        userService.checkUserExist(memberId);
        JmLogout logOutByUid = logoutService.getLogOutByUid(memberId);
        if (ObjectUtil.isEmpty(logOutByUid)||logOutByUid.getStatus().equals(CheckStatus.未通过)) {
            JmLogout logout = ObjectUtil.isEmpty(logOutByUid)?new JmLogout():logOutByUid;
            logout.setCreateTime(LocalDateTime.now());
            logout.setApplyReason(applyReason);
            logout.setUid(memberId);
            logout.setStatus(CheckStatus.待审核);
            boolean result = logoutService.saveOrUpdate(logout);
            if (result) return ResponseResult.success("申请成功");
        }
        return ResponseResult.error("申请失败");
    }


    @GetMapping("/get_my_log_out_status")
    @ApiOperation(value = "我的注销状态")
    public ResponseResult getMyLogOut() {
        Integer memberId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        userService.checkUserExist(memberId);
        JmLogout logOutByUid = logoutService.getLogOutByUid(memberId);
        Integer status = ObjectUtil.isEmpty(logOutByUid) ? 3 : logOutByUid.getStatus().getValue();
        return ResponseResult.success(status);
    }

    @GetMapping("/user_logout")
    @ApiOperation(value = "用户退出")
    @Transactional
    public ResponseResult logOut() {
        Integer memberId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        String client = ServletUtils.getStrHeader(JwtAdminInfo.loginClient);
        String tokenKey = ServletUtils.getStrHeader(JwtAdminInfo.tokenKey);
        String jpushRegisterId = ServletUtils.getStrHeader(JwtAdminInfo.jpushRegisterId);
        userService.checkUserExist(memberId);
        if(StringUtils.isNotBlank(jpushRegisterId)){
            UpdateWrapper wrapper=new UpdateWrapper();
            wrapper.set("status",0);
            wrapper.eq("register_id",jpushRegisterId);
            jpushUserService.update(wrapper);
        }
        boolean flag = redisService.deleteObject(tokenKey);
        if (client.equals("xcx")) {
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.set("wx_openid", "");
            wrapper.set("weixin_unionid", "");
            wrapper.eq("id", memberId);
            flag = userService.update(wrapper);
        }

        return ResponseResult.success(flag);
    }

}
